package com.medical.sys.web.controller;

import com.medical.common.JsonResult;
import com.medical.sys.model.Role;
import com.medical.sys.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.org.mozilla.javascript.internal.ast.TryStatement;

import java.util.List;

@Controller
@RequestMapping("/role")
//角色管理
public class RoleController {
    @Autowired
    private RoleService roleService;


    //根据id返回role对象
    @ApiOperation(value="根据id返回role对象", notes="根据id返回role对象")
    @RequestMapping(value="/getone/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getOne(@PathVariable String id){
        try {
            Role role = roleService.selectByPrimaryKey(id);
            return new JsonResult("200", "", role);
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }

    //返回role的集合
    @ApiOperation(value="返回role的集合", notes="返回role的集合")
    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getRole(){
        return new JsonResult(roleService.getRoles());
    }


    //新增
    @ApiOperation(value="新增", notes="新增")
    @RequestMapping(value = "/post",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult postRole(@RequestParam("name")String name,@RequestParam("description")String description,
                               @RequestParam("resourceIds")List resourceIds ) {
        try {
            Role role=new Role();
            role.setName(name);
            role.setDescription(description);
            role.setAvailable(true);
            role.setResourceIds(resourceIds);
            roleService.saveRole(role);
            return new JsonResult("200","",role);
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }

    //编辑
    @ApiOperation(value="根据id编辑role对象", notes="根据id编辑role对象")
    @RequestMapping(value = "/patch/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult patchRole(@PathVariable String id,@RequestParam("name") String name,
                                @RequestParam("description")String description,
                                @RequestParam("available")String available,@RequestParam("resourceIds")List resourceIds){
        try{
            Role role=roleService.selectByPrimaryKey(id);
            role.setName(name);
            role.setDescription(description);
            if (available.equals("true")){
                role.setAvailable(true);
            }else {
                role.setAvailable(false);
            }
            role.setResourceIds(resourceIds);
            roleService.updateRole(role);
            return new JsonResult("200","",role);
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }

    //删除
    @ApiOperation(value="删除", notes="删除")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable String id) {
        try {
            roleService.deleteRole(id);
            return new JsonResult("200", "删除成功");
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }
}


